/**
 * 
 */
package pl.koziolweb.wfm.client.components;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;

/**
 * @author bartlomiejk
 * 
 */
public class FileTreeComponent extends Composite {

	private Label path;

	private Tree tree;

	private TreeServiceAsync treeServiceAsync = (TreeServiceAsync) GWT
			.create(TreeService.class);

	private ServiceDefTarget endpoint = (ServiceDefTarget) treeServiceAsync;

	public FileTreeComponent() {
		endpoint.setServiceEntryPoint(GWT.getModuleBaseURL() + "TreeService");
		DockPanel dockPanel = new DockPanel();
		path = new Label();
		dockPanel.add(path, DockPanel.NORTH);
		tree = new Tree();
		tree.addSelectionHandler(new SelectionHandler<TreeItem>() {

			@Override
			public void onSelection(SelectionEvent<TreeItem> event) {
				final FileTreeItem fileTreeItem = (FileTreeItem) event
						.getSelectedItem();

				AsyncCallback<File[]> getListFileCallback = new AsyncCallback<File[]>() {
					public void onSuccess(File[] result) {
						if (result != null && result.length > 0) {
							for (File f : result) {
								FileTreeItem item = new FileTreeItem(f);
								fileTreeItem.addItem(item);
								if (f.isDirectory()) {
									item.addItem("");
								}
							}
						}
					}

					public void onFailure(Throwable ex) {
						error();
					}
				};
				fileTreeItem.removeItems();
				treeServiceAsync.fileList(fileTreeItem.getFile(),
						getListFileCallback);

				path.setText(fileTreeItem.getFile().getPath() + "/"
						+ fileTreeItem.getFile().getName());
			}
		});
		tree.addOpenHandler(new OpenHandler<TreeItem>() {
			@Override
			public void onOpen(OpenEvent<TreeItem> event) {
				final FileTreeItem fileTreeItem = (FileTreeItem) event
						.getTarget();

				AsyncCallback<File[]> getListFileCallback = new AsyncCallback<File[]>() {
					public void onSuccess(File[] result) {
						if (result != null && result.length > 0) {
							for (File f : result) {
								FileTreeItem item = new FileTreeItem(f);
								fileTreeItem.addItem(item);
								if (f.isDirectory()) {
									item.addItem("");
								}
							}
						}
					}

					public void onFailure(Throwable ex) {
						error();
					}
				};
				fileTreeItem.removeItems();
				treeServiceAsync.fileList(fileTreeItem.getFile(),
						getListFileCallback);

				path.setText(fileTreeItem.getFile().getPath() + "/"
						+ fileTreeItem.getFile().getName());
			}
		});
		dockPanel.add(tree, DockPanel.CENTER);
		initWidget(dockPanel);
	}

	@Override
	protected void onLoad() {
		AsyncCallback<File> getRootCallback = new AsyncCallback<File>() {
			public void onSuccess(File result) {
				path.setText(result.getName());
				FileTreeItem rootItem = new FileTreeItem(result);
				tree.addItem(rootItem);
				rootItem.addItem("");
			}

			public void onFailure(Throwable ex) {
				error();
			}
		};
		treeServiceAsync.getRoot(getRootCallback);
	}

	public native void error()/*-{
			alert("Something wrong...");
		}-*/;

}
